<?php

namespace frontend\models;

use Yii;

/**
 * This is the model class for table "dm_hang_hoa".
 *
 * @property integer $id
 * @property integer $nhom_id
 * @property string $ma
 * @property string $ten
 * @property integer $dvt_id
 * @property integer $ngay_tao
 *
 * @property DmDvt $dvt
 * @property DmNhomHang $nhom
 */
class DmHangHoa extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'dm_hang_hoa';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['nhom_id', 'ma', 'ten', 'dvt_id'], 'required'],
            [['nhom_id', 'dvt_id', 'ngay_tao'], 'integer'],
            [['ma'], 'string', 'max' => 10],
            [['ten'], 'string', 'max' => 100]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'nhom_id' => 'Nhom ID',
            'ma' => 'Ma',
            'ten' => 'Ten',
            'dvt_id' => 'Dvt ID',
            'ngay_tao' => 'Ngay Tao',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getDvt()
    {
        return $this->hasOne(DmDvt::className(), ['id' => 'dvt_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getNhom()
    {
        return $this->hasOne(DmNhomHang::className(), ['id' => 'nhom_id']);
    }
	/*
	*
	*/
	public static function getDmHangHoaByNhom($nhomId = false){
		$sql = "SELECT hh.*,nh.ma ma_nhom,nh.ten ten_nhom
				FROM dm_hang_hoa hh
				JOIN dm_nhom_hang nh ON hh.nhom_id = nh.id
				WHERE hh.trang_thai = 'Y'";
		if($nhomId)
			$sql .=	" AND nh.id IN ($nhomId) ";
		$sql .= " ORDER BY nh.ten";
		$data =	static::findBySql($sql)->asArray()->all();
		// echo '<pre>';
		// print_r($data);die;
		return $data;
	}
}
